package com.boomcat.boomcat.controller;

import com.alibaba.fastjson.JSONObject;
import com.boomcat.boomcat.domain.User;
import com.boomcat.boomcat.result.ExceptionCode;
import com.boomcat.boomcat.result.Result;
import com.boomcat.boomcat.service.UserService;
import com.boomcat.boomcat.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "*")
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("/register")
    Result register(@RequestBody JSONObject jsonObject) {
        System.out.println("register");

        String mailAddress = (String) jsonObject.get("mailAddress");
        String password = (String) jsonObject.get("password");
        String code = (String) jsonObject.get("code");
        String nickname = (String) jsonObject.get("nickname");


        //check password length and legal
        if(password.length()< 8){
            return Result.error(ExceptionCode.TOO_SHORT_PASSWORD);
        }
        else if(password.length() > 20){
            return Result.error(ExceptionCode.TOO_LONG_PASSWORD);
        }
        //密码不能含有中文
        boolean hasIllegalChar = !password.matches("^[a-zA-Z0-9]*$");
        if (hasIllegalChar) {
            return Result.error(ExceptionCode.ILLEGAL_PASSWORD);
        }

        //check nickname length and legal
        boolean nicknameIsPureChars=nickname.matches("^[a-zA-Z0-9]*$");
        if (nicknameIsPureChars) {
            //English can be 4-10
            if(nickname.length() > 10){
                return Result.error(ExceptionCode.TOO_LONG_NICKNAME);
            }
            else if (nickname.length() < 4) {
                return Result.error(ExceptionCode.TOO_SHORT_NICKNAME);
            }
        }
        else{
            //Chinese can be 1-7
            if(nickname.length() > 7){
                return Result.error(ExceptionCode.TOO_LONG_NICKNAME);
            }
            else if (nickname.length() < 1) {
                return Result.error(ExceptionCode.TOO_SHORT_NICKNAME);
            }
        }
        boolean hasSpecialChar = !nickname.matches("^[a-zA-Z0-9\u4e00-\u9fa5]*$");
        if (hasSpecialChar){
            return Result.error(ExceptionCode.ILLEGAL_NICKNAME);
        }

        User user=new User();
        user.setName(nickname);
        user.setAccount(mailAddress);
        user.setPassword(password);
        userService.register(user);
        return Result.success();
    }

    @PostMapping("/login")
    Result login(@RequestBody JSONObject jsonObject) {
        System.out.println("login");

        String account = (String) jsonObject.get("account");
        String password = (String) jsonObject.get("password");
        System.out.println("account="+account+" password="+password);

//        select user in db
        User user = userService.selectUser(account, password);
        if (user != null) {
            System.out.println("登陆成功");
            JSONObject resp=new JSONObject();

            String token = JwtUtil.createJwt(user.getUserId().toString());
            System.out.println("token="+token);
            resp.put("token",token);
            resp.put("name",(user.getName()));
            resp.put("point",(user.getPoint()));
            resp.put("avatar",user.getAvatar());
            return Result.success(resp);
        }
        return Result.error(ExceptionCode.WRONG_PASSWORD);
    }

//    @PostMapping("/getVerificationCode")
//    Result getVerificationCode(@RequestBody JSONObject jsonObject) {
//        System.out.println("getVerificationCode");
//
//        String mailAddress = (String) jsonObject.get("mailAddress");
////        System.out.println("mailbox="+ mailAddress);
//        //check the mailbox form, if false then inform the error message
//        boolean mailAddressOk = userService.verifyMailAddress(mailAddress);
//        if(!mailAddressOk) {
//            return Result.error(ExceptionCode.ILLEGAL_ACCOUNT);
//        }
////        // select mailAddress from db
//        User user = userService.selectUserByMailAddress(mailAddress);
//        if (user != null) {
//            return Result.error(ExceptionCode.ALREADY_REGISTERED);
//        }
//
//        //generate the code and send email but not return to the front end
//        try {
//            userService.sendCaptchaEmail(mailAddress);
//        } catch (MessagingException e) {
//            //if we can't send the code email, we can say that the address is non-existent
//            return Result.error(ExceptionCode.NON_EXISTENT_ACCOUNT);
//        }
//        System.out.println("验证码已经发送");
//        return Result.success();
//    }
//
//    @GetMapping("/getRandomNickname")
//    Result getRandomNickname()  {
//        System.out.println("getRandomNickname");
//        String nickname = userService.getRandomNickname();
//        JSONObject jsonobject=new JSONObject();
//        jsonobject.put("nickname",nickname);
//        return Result.success(jsonobject);
//    }
}
